package org.quasar.airNova.presentationLayer;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.quasar.airNova.businessLayer.Bilhete;
import org.quasar.airNova.businessLayer.CalendarDate;
import org.quasar.airNova.businessLayer.Reserva;
import org.quasar.airNova.persistenceLayer.Database;

public class TrocarBilhete extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField nLugarJTF;
	private Gui gui;
	private JComboBox nBilhetes;
	private JComboBox nReserva;
	private JComboBox nCliente;

	/**
	 * Create the panel.
	 * 
	 * @param gui
	 */
	public TrocarBilhete(Gui gui) {
		setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel Bilhetes = new JLabel("Bilhetes");
		Bilhetes.setBounds(40, 117, 126, 14);
		panel.add(Bilhetes);

		JLabel lblNewLabel_1 = new JLabel("Cliente");
		lblNewLabel_1.setBounds(40, 67, 126, 14);
		panel.add(lblNewLabel_1);

		JLabel lblLugar = new JLabel("Novo Lugar");
		lblLugar.setBounds(40, 142, 126, 14);
		panel.add(lblLugar);

		nLugarJTF = new JTextField();
		nLugarJTF.setBounds(231, 139, 203, 20);
		panel.add(nLugarJTF);
		nLugarJTF.setColumns(10);

		JButton btnNewButton_2 = new JButton("Apagar Campos");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nLugarJTF.setText("");
			}
		});
		btnNewButton_2.setBounds(251, 217, 126, 23);
		panel.add(btnNewButton_2);

		JButton btnNewButton = new JButton("Submeter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (nCliente.getSelectedItem() != null
						&& nReserva.getSelectedItem() != null
						&& nBilhetes.getSelectedItem() != null
						&& nLugarJTF.getText().length() != 0) {
					AirNova_Database.trocarBilhete(
							(String) nCliente.getSelectedItem(),
							(String) nReserva.getSelectedItem(),
							(String) nBilhetes.getSelectedItem(),
							nLugarJTF.getText());
					generateComboBoxBilhetes((String) nReserva
							.getSelectedItem());
				}
			}
		});
		btnNewButton.setBounds(44, 217, 98, 23);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Cancelar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(
						getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(152, 217, 89, 23);
		panel.add(btnNewButton_1);

		JLabel lblNewLabel_3 = new JLabel("Trocar Bilhetes duma reserva");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 243, 32);
		panel.add(lblNewLabel_3);

		if (Tools.getClienteCodigos() != null) {
			nCliente = new JComboBox(Tools.getClienteCodigos());
		} else
			nCliente = new JComboBox();
		nCliente.setBounds(231, 64, 203, 20);
		nCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				generateComboBoxReservas((String) nCliente.getSelectedItem());
				if (nReserva.getSelectedItem() != null)
					generateComboBoxBilhetes((String) nReserva
							.getSelectedItem());
			}
		});
		panel.add(nCliente);

		nReserva = new JComboBox();
		if (nCliente.getSelectedItem() != null) {
			generateComboBoxReservas((String) nCliente.getSelectedItem());
		}
		nReserva.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				generateComboBoxBilhetes((String) nReserva.getSelectedItem());
			}
		});

		nBilhetes = new JComboBox();
		if (nReserva.getSelectedItem() != null)
			generateComboBoxBilhetes((String) nReserva.getSelectedItem());

		nBilhetes.setBounds(231, 114, 203, 20);
		panel.add(nBilhetes);

		nReserva.setBounds(231, 89, 203, 20);
		panel.add(nReserva);

		JLabel Reserva = new JLabel("Reserva");
		Reserva.setBounds(40, 92, 151, 14);
		panel.add(Reserva);

	}

	public Gui getGui() {
		return gui;
	}

	private void generateComboBoxBilhetes(String nReserva) {
		if (nReserva != null) {
			Reserva r = Tools.getReservaByN_Reserva(nReserva);
			nBilhetes.removeAllItems();
			for (Bilhete each : r.bilhetes()) {
				nBilhetes.addItem("" + each.lugar());
			}
		}
	}

	private void generateComboBoxReservas(String nCliente) {
		Set<Reserva> reservas = Tools.getAllReservasCliente(nCliente);
		nReserva.removeAllItems();

		for (Reserva each : reservas) {
			nReserva.addItem("" + each.n_reserva());
		}
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}

}
